package org.util.properties;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class XMLProcessor {
	protected SAXBuilder sb;
	protected Document doc;
	protected Element root;
	static Logger logger = Logger.getLogger(XMLProcessor.class.getName());

	public XMLProcessor() {
	}

	public XMLProcessor(String filePath) throws Exception {
		this.sb = new SAXBuilder();
		try {
			InputStream inputstream = getClass().getResourceAsStream(filePath);
			this.doc = this.sb.build(inputstream);
			this.root = this.doc.getRootElement();
		} catch (Exception e) {
			logger.error(null, e);
			throw new Exception("!!! 属性处理器构造失败");
		}
	}

	public List getPropertyList(String propertyName) {
		List result = new ArrayList();
		Element property = getElement(this.root, "Name", propertyName);
		if (property == null)
			return null;
		Iterator iterator = property.getChildren().iterator();
		while (iterator.hasNext()) {
			Map tmpMap = new HashMap();
			Element tmp = (Element) iterator.next();
			Iterator tmpi = tmp.getAttributes().iterator();
			while (tmpi.hasNext()) {
				Attribute tmpa = (Attribute) tmpi.next();
				tmpMap.put(tmpa.getName(), tmpa.getValue());
			}
			result.add(tmpMap);
		}
		if (result.isEmpty())
			return null;
		return result;
	}

	public List getList(Element element) {
		if (element == null)
			return null;
		List result = new ArrayList();
		Iterator iterator = element.getChildren().iterator();
		while (iterator.hasNext()) {
			Map tmpMap = new HashMap();
			Element tmpElement = (Element) iterator.next();
			Iterator tmpIt = tmpElement.getAttributes().iterator();
			while (tmpIt.hasNext()) {
				Attribute tmpAtt = (Attribute) tmpIt.next();
				tmpMap.put(tmpAtt.getName(), tmpAtt.getValue());
			}
			result.add(tmpMap);
		}
		if (result.isEmpty())
			return null;
		return result;
	}

	public List getListRecursion(Element element) {
		if (element == null)
			return null;
		List result = new ArrayList();
		Iterator it = element.getChildren().iterator();
		while (it.hasNext()) {
			Element e = (Element) it.next();
			getListRecursion2(e, result);
		}
		return result;
	}

	private void getListRecursion2(Element element, List result) {
		if (element.getName().equals("e")) {
			Map mp = new HashMap();
			mp.put("Key", element.getAttributeValue("Key"));
			mp.put("Value", element.getAttributeValue("Value"));
			result.add(mp);
		}
		Iterator it = element.getChildren().iterator();
		while (it.hasNext()) {
			Element sube = (Element) it.next();
			getListRecursion2(sube, result);
		}
	}

	public Element getElement(Element baseElement, String attributeName,
			String attributeValue) {
		Iterator iterator = baseElement.getChildren().iterator();
		while (iterator.hasNext()) {
			Element tmp = (Element) iterator.next();
			if (tmp.getAttributeValue(attributeName).equals(attributeValue)) {
				return tmp;
			}
		}
		return null;
	}

	public Element getElementRecursion(Element baseElement,
			String attributeName, String attributeValue) {
		Iterator it = baseElement.getChildren().iterator();
		while (it.hasNext()) {
			Element tmp = (Element) it.next();
			Element re = getElementRecursion2(tmp, attributeName,
					attributeValue);
			if (re != null)
				return re;
		}
		return null;
	}

	private Element getElementRecursion2(Element base, String attributeName,
			String attributeValue) {
		String value = base.getAttributeValue(attributeName);
		if ((value != null) && (value.equals(attributeValue))) {
			return base;
		}
		Iterator it = base.getChildren().iterator();
		while (it.hasNext()) {
			Element tmp = (Element) it.next();
			Element re = getElementRecursion2(tmp, attributeName,
					attributeValue);
			if (re != null)
				return re;
		}
		return null;
	}

	public Element getParent(Element child) {
		if (System.getProperty("java.vm.version").startsWith("1.4")) {
			return (Element) child.getParent();
		}
		return child;
	}
}
